#-*- coding: utf-8 -*-
'''
Created on Aug 27, 2012

@author: bbg
'''

from sqlalchemy import Integer, Column, String, MetaData, Table, DateTime
from sqlalchemy.orm import mapper
    
class OrderMain(object):
    '''
        OrderMain信息
    '''
    _Mapper_ = None
    _company_id_ = -1
    _shop_id_ = -1    
    def __init__(self):
        self.order_id = ''
        self.room_alias_id = ''
        self.room_mark = ''
        self.person_count = 0
        self.amount = 0
        self.room_state = 0
        self.open_time = ''
        self.close_time = ''
        self.final_amount = 0
        self.discount_rate = 0
        self.discount_amount = 0
        self.free_amount = 0
        self.service_fee_rate = 0
        self.service_amount = 0
        self.opener = ''
        self.casher = ''
        self.extra_amount = 0
        self.create_time = ''
        self.modify_time = ''
        
    @staticmethod
    def doMapping(company_id, shop_id, year):
        '''
        按company_id分库，shop_id对应公司分店，每次生成对象前都要执行一次，以便map到正确的数据库上去
        '''
        if OrderMain._company_id_ == company_id and OrderMain._shop_id_ == shop_id:
            return
                
        if OrderMain._Mapper_ is not None:
            OrderMain._Mapper_.dispose()
            
        metaData = MetaData()
        tableName = 'order_main_%s' % year
        orderMainTable = Table(tableName, metaData,
                           Column('order_id', String, primary_key = True),
                           Column('room_alias_id', String),
                           Column('room_mark', String),
                           Column('person_count', Integer),
                           Column('amount', Integer),
                           Column('room_state', Integer),
                           Column('open_time', DateTime),
                           Column('close_time', DateTime),
                           Column('final_amount', Integer),
                           Column('discount_rate', Integer),
                           Column('discount_amount', Integer),
                           Column('free_amount', Integer),
                           Column('service_fee_rate', Integer),
                           Column('service_amount', Integer),
                           Column('opener', String),
                           Column('casher', String),
                           Column('extra_amount', Integer),
                           Column('create_time', String),
                           Column('modify_time', String),
                           schema = "shop_order_%s_%s" % (company_id, shop_id))
        
        OrderMain._Mapper_ = mapper(OrderMain, orderMainTable)
    